package com.thunder.rebot.rebotclient.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.thunder.rebot.rebotclient.R;
import com.thunder.rebot.rebotclient.activity.TestActivity;
import com.thunder.rebot.rebotclient.entity.PersonEntity;

import java.util.ArrayList;
/*
显示人的列表
 */

public class PersonAdapter extends BaseAdapter {


    private Context context;
    private int res ;
    private ArrayList<PersonEntity> list;

    public PersonAdapter(){}

    public PersonAdapter(Activity testActivity, ArrayList<PersonEntity> list, int   res) {
        this.context = testActivity;
        this.res = res;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    private  ViewHolder viewHolder;

    public ViewHolder getViewHolder() {
        return viewHolder;
    }

    public void setViewHolder(ViewHolder viewHolder) {
        this.viewHolder = viewHolder;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
//        LayoutInflater inflater = LayoutInflater.from(context);
//        View view = inflater.inflate(res,null);
//        final TextView textView = (TextView) view.findViewById(R.id.tvPersonName);
//        ImageView imageView = (ImageView) view.findViewById(R.id.ivPersonIcon);
//        textView.setText(list.get(position).getName());
//        return view;

        if(convertView == null){
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(res, null);
            viewHolder.textView = (TextView)convertView.findViewById(R.id.tvPersonName);
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.ivPersonIcon);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder)convertView.getTag();
        }
        viewHolder.textView.setText(list.get(position).getName());

        if(list.get(position).getState() == 1){
            viewHolder.imageView.setImageDrawable(context.getResources().getDrawable(R.mipmap.start_person));
        }else if(list.get(position).getState() == 2){
            viewHolder.imageView.setImageDrawable(context.getResources().getDrawable(R.mipmap.end_person));
        }else{
            viewHolder.imageView.setImageDrawable(context.getResources().getDrawable(R.mipmap.default_person));
        }


        return convertView;
    }
    public class ViewHolder{
        TextView textView;
        ImageView imageView;
    }
}
